package top.go2do.auth.business.user.controller;

import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.go2do.auth.business.captcha.service.CaptchaService;
import top.go2do.auth.business.user.module.LoginCheckBO;
import top.go2do.auth.business.user.module.LoginResultVO;
import top.go2do.auth.business.user.module.dto.LoginDTO;
import top.go2do.auth.business.user.service.LoginService;
import top.go2do.auth.business.user.service.decorator.CaptchaLoginDecorator;
import top.go2do.auth.configProperties.AuthModuleConfig;

/**
 * @author xingyuji
 * @date 2025/3/9 14:06
 * @description
 */
@RestController
@Slf4j
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AuthModuleConfig authConfig;

    @Autowired
    private CaptchaService captchaService;

    @PostMapping("/login")
    LoginResultVO login(@Valid @RequestBody LoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        log.info("login username:{}", username);
        /**
         * step1: 密码（密文）解密后
         */
        String passwordHash = loginDTO.getPasswordHash();
        // TODO: 解密得到明文密码
        String passwordRaw = passwordHash;

        LoginCheckBO checkBO = new LoginCheckBO();
        BeanUtils.copyProperties(loginDTO, checkBO);
        checkBO.setPasswordRaw(passwordRaw);
        loginService = new CaptchaLoginDecorator(loginService, captchaService, authConfig, redisTemplate);
        return loginService.toLoginWithToken(checkBO);
    }

}
